package com.example.jack.community.view.home.homeAdapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.jack.community.R;
import com.example.jack.community.data.HomeItemBean1;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

import static android.R.string.no;


/**
 * Created by vveng on 2017/10/30 14: 43
 *
 * @mail: vvengStruggle@163.com
 * 首页动态适配器
 */

public class HomeItemAdapter2 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private LayoutInflater mInflater;
    private Context mContext;
    private ArrayList<HomeItemBean1> bean;
    private HomeItemRcAdapter mItemRcAdapter;

    //上拉加载更多
    public static final int  PULLUP_LOAD_MORE=0;
    //正在加载中
    public static final int  LOADING_MORE=1;
    //上拉加载更多状态-默认为0
    private int load_more_status=0;

    private static final int TYPE_ITEM = 0;  //普通Item View
    private static final int TYPE_FOOTER = 1;  //顶部FootView


    public void setData(ArrayList<HomeItemBean1> mbean) {
        this.bean = mbean;
        notifyDataSetChanged();
    }

    public HomeItemAdapter2(Context context) {
        mContext = context;
    }

    public HomeItemAdapter2(Context context, ArrayList<HomeItemBean1> mdatas) {
        mContext = context;
        this.bean = mdatas;
        this.mInflater = LayoutInflater.from(context);
        notifyDataSetChanged();
    }


    public interface OnItemClikeListener {
        void onItemClike(int position, HomeItemBean1 homeItemBean1);

        void onItemPhotoCilck(View view, ArrayList<String> urls, int position);
    }

    private OnItemClikeListener mOnItemClikeListener;

    public void setOnItemClikeListener(OnItemClikeListener onItemClikeListener) {
        mOnItemClikeListener = onItemClikeListener;
    }

    public void updateItemData(ArrayList<HomeItemBean1> datas){
        //先清除数据在加载数据
        bean.removeAll(bean);
        bean.addAll(datas);
      notifyDataSetChanged();
    }
    public void notifyData(int position,HomeItemBean1 bean1){
        bean.set(position,bean1);
       notifyItemChanged(position);
    }

    public void addItemData(ArrayList<HomeItemBean1> datas) {
        bean.addAll(datas);
        //     load_more_status=status;
        notifyDataSetChanged();
    }

    public void clearItemData() {
        bean.clear();
    }

    /**
     * //上拉加载更多
     * PULLUP_LOAD_MORE=0;
     * //正在加载中
     * LOADING_MORE=1;
     * //加载完成已经没有更多数据了
     * NO_MORE_DATA=2;
     * @param status
     */
    public void changeMoreStatus(int status){
        load_more_status=status;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return bean.size() + 1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //判断是普通item还是底部局
        if(viewType==TYPE_ITEM){
            View view = mInflater.inflate(R.layout.home_item1, parent, false);
            final MyViewHolder holder = new MyViewHolder(view);

            return holder;
        }else if(viewType == TYPE_FOOTER){
            View foot_view=mInflater.inflate(R.layout.footer_layout,parent,false);
            //这边可以做一些属性设置，甚至事件监听绑定
            //view.setBackgroundColor(Color.RED);
            FootViewHolder footViewHolder=new FootViewHolder(foot_view);
            return  footViewHolder;
        }

        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        if (holder instanceof MyViewHolder){
            Glide.with(mContext).load(bean.get(position).getAvatarImage()).into(((MyViewHolder)holder).AvatarImage);
            ((MyViewHolder)holder).username.setText(bean.get(position).getUsername());
            ((MyViewHolder)holder).time.setText(bean.get(position).getTime());
            ((MyViewHolder)holder).context.setText(bean.get(position).getContext());
            ((MyViewHolder) holder).nice_tv.setText(bean.get(position).getNice());
            ((MyViewHolder) holder).browse_tv.setText(bean.get(position).getBrowse());
            ((MyViewHolder) holder).comment_tv.setText(bean.get(position).getComment());
            final boolean niceFlag = bean.get(position).getNiceFlag();
            if(niceFlag){
                ((MyViewHolder)holder).nice_image.setImageResource(R.drawable.ic_heard_true4);
            }else{
                ((MyViewHolder)holder).nice_image.setImageResource(R.drawable.ic_heard_false4);
            }
            //点赞逻辑，待优化
            ((MyViewHolder)holder).nice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(bean.get(position).getNiceFlag()){
                        ((MyViewHolder)holder).nice_image.setImageResource(R.drawable.ic_heard_false4);
                        bean.get(position).setNiceFlag(false);
                        String s = ((MyViewHolder) holder).nice_tv.getText().toString();
                        int i = Integer.parseInt(s);  i--;
                        String s1 = String.valueOf(i);
                        ((MyViewHolder) holder).nice_tv.setText(s1);
                        bean.get(position).setNice(s1);
                    }else {
                        ((MyViewHolder)holder).nice_image.setImageResource(R.drawable.ic_heard_true4);
                        bean.get(position).setNiceFlag(true);
                        String s = ((MyViewHolder) holder).nice_tv.getText().toString();
                        int i = Integer.parseInt(s); i++;
                        String s1 = String.valueOf(i);
                        ((MyViewHolder) holder).nice_tv.setText(s1);
                        bean.get(position).setNice(s1);
                    }
                }
            });
            //获取图片的地址
            ArrayList<String> urls = bean.get(position).getUrls();
            mItemRcAdapter = new HomeItemRcAdapter(mContext,urls);
            //判断只有一张还是几张
            if(urls.size()==1){
                ((MyViewHolder)holder).mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
                ((MyViewHolder)holder).mRecyclerView.setAdapter(mItemRcAdapter);
            }else{
                ((MyViewHolder)holder).mRecyclerView.setLayoutManager(new GridLayoutManager(mContext,3));
                ((MyViewHolder)holder).mRecyclerView.setAdapter(mItemRcAdapter);
            }

            mItemRcAdapter.setOnRcItemListener(new HomeItemRcAdapter.OnRcItemListener() {
                @Override
                public void onRcItemClick(View view, ArrayList<String> urls, int posistion) {
                    //这里回调的是整个item的监听，不是item里面的rc的item
                    //  mOnItemClikeListener.onItemClike(holder.itemView,mPosition,bean.get(mPosition).getUserId());
                    mOnItemClikeListener.onItemPhotoCilck(view,urls,posistion);
                }
            });
            //设置卡片布局
            ((MyViewHolder) holder).mCardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int mPosition = holder.getLayoutPosition();
                    mOnItemClikeListener.onItemClike(mPosition,bean.get(mPosition));
                }
            });

        }else if(holder instanceof  FootViewHolder){
            FootViewHolder footViewHolder=(FootViewHolder)holder;
            switch (load_more_status){
                case PULLUP_LOAD_MORE:
                    footViewHolder.foot_view_item_pb.setVisibility(View.GONE);
                    footViewHolder.foot_view_item_tv.setText("上拉加载更多...");
                    break;
                case LOADING_MORE:
                    footViewHolder.foot_view_item_pb.setVisibility(View.VISIBLE);
                    footViewHolder.foot_view_item_tv.setText("正在加载更多数据...");
                    break;
            }
        }
    }



    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private RecyclerView mRecyclerView;
        private TextView username, time, context;
        private TextView nice_tv,comment_tv,browse_tv;
        private CircleImageView AvatarImage;
        private LinearLayout nice;
        private ImageView nice_image;
        private CardView mCardView;

        public MyViewHolder(View view) {
            super(view);

            AvatarImage = (CircleImageView) view.findViewById(R.id.home1_item_tx);
            mRecyclerView = (RecyclerView) view.findViewById(R.id.home1_item_images);
            username = (TextView) view.findViewById(R.id.home1_item_username);
            time = (TextView) view.findViewById(R.id.home1_item_time);
            context = (TextView) view.findViewById(R.id.home1_item_context);
            nice = (LinearLayout) view.findViewById(R.id.home_item1_nice);
            nice_image = (ImageView) view.findViewById(R.id.home1_image_nice);
            nice_tv = (TextView) view.findViewById(R.id.home1_item_tv);
            comment_tv = (TextView) view.findViewById(R.id.home1_item_comment);
            browse_tv = (TextView) view.findViewById(R.id.home1_item_browse);
            mCardView= (CardView) view.findViewById(R.id.home1_cardView);

        }
    }

    /**
     * 底部FootView布局
     */
    public static class FootViewHolder extends RecyclerView.ViewHolder {
        private TextView foot_view_item_tv;
        private ProgressBar foot_view_item_pb;
        public FootViewHolder(View view) {
            super(view);

            foot_view_item_tv = (TextView) view.findViewById(R.id.home_item_foot_tv);
            foot_view_item_pb = (ProgressBar) view.findViewById(R.id.home_item_foot_pb);
        }
    }

}
